import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

class Request {
    public String username;
    public String password;
}

class Response {
    public boolean ok;
}

@WebServlet("/json")
public class JsonParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //把请求中的 body 按照 json 格式解析成 java 对象
//        ObjectMapper objectMapper = new ObjectMapper();
//        Request request = objectMapper.readValue(req.getInputStream(), Request.class);
//        System.out.println("username:" + request.username + ", password:" + request.password);
//        Response response = new Response();
//        response.ok = true;

        ObjectMapper objectMapper = new ObjectMapper();

        Request request = new Request();
        request.password = "1234";
        request.username = "lisi";

        //把响应对象转成 json 字符串
        String respJson = objectMapper.writeValueAsString(request);

        resp.setContentType("application/Json;charset=utf-8");
        resp.getWriter().write(respJson);
        System.out.println(respJson);
    }
}
